<?php
namespace app\common\model;

use think\Model;

class GoodsCategory extends Model
{
	// 验证规则
	protected $rule = [
		['goods_category_parent_id|父分类', ['require']],
		['goods_category_name|分类名称', ['require']],
	]; 
	
	// 验证场景
	protected $scene = [
        'insert' => ['goods_category_parent_id', 'goods_category_name', ''],
        'update' => ['goods_category_name', ''],
    ];

    // 验证器
	public function intValidate(){
        $validate = new \think\Validate($this->rule);
        foreach ($this->scene as $k1=>$v1)
            $validate->scene($k1, $v1);
        return $validate;
    }
	// 获取主分类
	public static function getMainCategory()
	{
		$data = Db('goods_category')
            ->where('goods_category_parent_id', 0)
            ->where('goods_category_is_delete', 0)
            ->select();
		return $data;
	}

	//找一个分类下所有的子分类
	public static function getChild($catId){
		$data = GoodsCategory::where('goods_category_is_delete', 0)->select();
		return GoodsCategory::_getChild($data, $catId, true);
	}

	//递归从数据中找子分类
	private static function _getChild($data, $catId, $isClear = false)
	{
		//保存找到子分类的ID
		static $_ret = [];		
		if($isClear)
			$_ret = [];
		foreach($data as $k => $v){
			if($v['goods_category_parent_id'] == $catId){
				$_ret[] = $v['goods_category_id'];
				self::_getChild($data, $v['goods_category_id']);
			}
		}
		return $_ret;
	}
	public static function getOnlyChilds($catId){
		$data = GoodsCategory::where('goods_category_is_delete', 0)->select();
		return GoodsCategory::_getChilds($data, $catId, true);
	}

	//递归从数据中找子分类
	private static function _getChilds($data, $catId, $isClear = false)
	{
		//保存找到子分类的ID
		static $_ret = [];		
		if($isClear)
			$_ret = [];
		foreach($data as $k => $v){
			if($v['goods_category_parent_id'] == $catId){
				$_ret[] = $v['goods_category_id'];
			}
		}
		return $_ret;
	}

	

	//重新排序并打印树形结构
	public static function getTree(){
		$data = GoodsCategory::where('goods_category_is_delete', 0)->select();
		return GoodsCategory::_getTree($data);
	}


	private static function _getTree($data, $parent_id=0, $level=0)
	{
		static $ret = [];
		foreach($data as $k => $v){
			if($v['goods_category_parent_id'] == $parent_id)
			{
				//标记这个分类是第几级的
				$v['level'] = $level;	
				$ret[] = $v;
				self::_getTree($data, $v['goods_category_id'], $level+1);
			}
		}
		return $ret;
	}


	// 修改分类名称
	public static function updateCatName($id, $category_name)
	{
		$result = GoodsCategory::where('goods_category_id', $id)->setField('goods_category_name', $category_name);
		halt(GoodsCategory::getLastSql());
		if(!$result)
			return false;

		return true;
	}

	// 删除分类
	public static function changDel($catId)
	{
		$result = GoodsCategory::where('goods_category_id', $catId)->setField('goods_category_is_delete', 1);
		if(!$result)
		{
			return false;
		}
	
		return true;
	}



	public static function getParentCategory()
	{
		$data = GoodsCategory::where('goods_category_parent_id', 0)->where('goods_category_is_delete',0)->select();
		return $data;
	} 

	// 根据子分类的id获取子分类信息
	public static function getChildsInfo($catId)
	{
		$childsData = []; 
		$data = GoodsCategory::getChild($catId);
		foreach($data as $k => $v)
		{
			$result = GoodsCategory::field('goods_category_id,goods_category_name,goods_category_parent_id')->where('goods_category_is_delete',0)->find($v);
			$childsData[] = $result;
		}


		return $childsData;
	} 


	public function getNavData()
	{
			//取出所有的分类
			$allCat = GoodsCategory::all();
			$rt = [];
			//循环所有的分类
			foreach($allCat as $k => $v)
			{
				if($v['parent_id'] == 0)
				{
					//循环所有的分类找出这个顶级分类的子分类
					foreach($allCat as $k1 => $v1)
					{
						if($v1['parent_id'] == $v['id'])
						{
							// //循环所有的分类找出二级分类的子分类
							// foreach($allCat as $k2 => $v2)
							// {
							// 	if($v2['parent_id'] == $v1['id'])
							// 	{
							// 		$v1['childs'][] = $v2;
							// 	}
							// }
							$v['childs'][] = $v1;
						}
					}
					$rt[] = $v;
				}
			}
		return $rt;
	}
	// 面包屑导航
	public function parentPath($catId)
	{
		static $rs = array();
		$info = $this -> field('id,cat_name,parent_id') -> find($catId);
		$rs[] = $info;
		// dump($rs);die;
		//如果还有上级就再取上级的信息
		if($info['parent_id'] > 0)
		{
			$this -> parentPath($info['parent_id']);
		}
		return $rs;
	} 

}